﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using System.IO;
using System.Windows.Forms;

namespace AtletIS.Shared.Helpers
{
    public static class TotalResultsHelper
    {

        public static void ForDisciplineIdTable(int disciplineId, int competitionId, StreamWriter sw)
        {
            int disciplineCode = Convert.ToInt32(DatabaseHelper.GetDisciplinesById(disciplineId).Rows[0]["Code"]);
            DataTable dtPerformances = DatabaseHelper.GetPerformancesByDisciplineId(competitionId, disciplineId); 
            
            sw.WriteLine("<table class=\"total-table\">");
            sw.WriteLine("<tr height=\"30\" bgcolor=\"#D8D8D8\">");
            sw.WriteLine("  <th width=\"30\">Celk.</th>");
            sw.WriteLine("  <th width=\"30\">Pohl.</th>");
            sw.WriteLine("  <th width=\"30\">Kat.</th>");
            sw.WriteLine("  <th width=\"30\">Kat.</th>");
            sw.WriteLine("  <th width=\"40\">Št.č.</th>");
            sw.WriteLine("  <th>Meno</th>");
            sw.WriteLine("  <th width=\"40\">Nar.</th>");
            sw.WriteLine("  <th width=\"50\">Krajina</th>");
            sw.WriteLine("  <th>Klub</th>");
            sw.WriteLine("  <th width=\"60\">Čas</th>");
            sw.WriteLine("</tr>");

            int row = 1;
            foreach (DataRow dr in dtPerformances.Rows)
            {
                // Row color
                if (row % 2 == 1)
                    sw.WriteLine("<tr>");
                else
                    sw.WriteLine("<tr bgcolor=\"#CFECEC\">");

                sw.WriteLine("  <td align=\"center\">" + dr["TotalOrder"].ToString() + "</td>");
                sw.WriteLine("  <td align=\"center\">" + dr["SexOrder"].ToString() + "</td>");
                sw.WriteLine("  <td align=\"center\">" + dr["CategoryOrder"].ToString() + "</td>");
                sw.WriteLine("  <td align=\"center\">" + dr["CategoryCode"].ToString() + "</td>");
                sw.WriteLine("  <td align=\"center\">(" + dr["StartNumber"].ToString() + ")</td>");
                sw.WriteLine("  <td>" + dr["Surname"].ToString() + " " + dr["FirstName"].ToString() + "</td>");
                sw.WriteLine("  <td align=\"center\">" + Convert.ToDateTime(dr["Birthdate"]).Year.ToString() + "</td>");
                sw.WriteLine("  <td align=\"center\">" + dr["CountryId"].ToString() + "</td>");
                sw.WriteLine("  <td>" + dr["ClubName"].ToString() + "</td>");
                sw.WriteLine("  <td align=\"center\">" + PrintHelper.PrintResult(dr["Result"].ToString(), disciplineCode) + "</td>");

                sw.WriteLine("</tr>");
                row += 1;
            }

            sw.WriteLine("</table>");
        }

        public static void ForDisciplineId(int competitionId, int disciplineId, string disciplineName)
        {
            string path = Path.Combine("Ext", "total_results.html");
            StreamWriter sw = new StreamWriter(path, false, Encoding.Default);
            
            try
            {
                ResultsHelper.ResultsHeader(sw);
                sw.WriteLine("<h3>" + disciplineName + " - Absolútne poradie" + "</h3>");
                ForDisciplineIdTable(disciplineId, competitionId, sw);
                ResultsHelper.ResultsFooter(sw);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Absolútne poradie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                sw.Close();
                sw.Dispose();
                System.Diagnostics.Process.Start(path);
            }
        }





    }
}
